﻿Imports System.Web.UI

Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class af_sendto
        Inherits ForumBase
        Private bcUpdated As Boolean = False
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
            reqEmail.Text = "<img src=""" & ImagePath & "/warning.png"" />"
            reqMessage.Text = "<img src=""" & ImagePath & "/warning.png"" />"
            reqName.Text = "<img src=""" & ImagePath & "/warning.png"" />"
            reqSubject.Text = "<img src=""" & ImagePath & "/warning.png"" />"
            regEmail.Text = "<img src=""" & ImagePath & "/warning.png"" />"
            regEmail.ValidationExpression = "\b[a-zA-Z0-9._%\-+']+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,4}\b"
            Dim TopicSubject As String = String.Empty
            If TopicId > 0 Then
                Dim tc As New TopicsController
                Dim ti As TopicInfo = tc.Topics_Get(PortalId, ModuleId, TopicId, ForumId, UserId, True)
                If Not ti Is Nothing Then
                    If Permissions.HasPerm(ForumInfo.Security.Read, ForumUser.UserRoles) Then
                        If Not Page.IsPostBack Then
                            Dim SubjectDefault As String = GetSharedResource("[RESX:EmailSubjectDefault]")
                            TopicSubject = ti.Content.Subject
                            SubjectDefault = SubjectDefault.Replace("[SUBJECT]", ti.Content.Subject)

                            txtRecipSubject.Text = SubjectDefault
                            Dim MessageDefault As String = GetSharedResource("[RESX:EmailMessageDefault]")
                            Dim sURL As String = NavigateUrl(TabId, "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.ViewType & "=" & Views.Topic, ParamKeys.TopicId & "=" & TopicId})
                            If MainSettings.UseShortUrls Then
                                sURL = NavigateUrl(TabId, "", New String() {ParamKeys.TopicId & "=" & TopicId})
                            End If
                            MessageDefault = MessageDefault.Replace("[TOPICLINK]", sURL)
                            MessageDefault = MessageDefault.Replace("[DISPLAYNAME]", UserProfiles.GetDisplayName(ModuleId, UserId, MainSettings.UserNameDisplay, UserInfo.Username, UserInfo.FirstName, UserInfo.LastName, UserInfo.DisplayName))
                            txtMessage.Text = MessageDefault
                        End If

                    End If
                    If MainSettings.UseSkinBreadCrumb Then
                        Dim sCrumb As String = "<a href=""" & NavigateUrl(TabId, "", ParamKeys.GroupId & "=" & ForumGroupId) & """>" & ForumInfo.GroupName & "</a>|"
                        If MainSettings.UseShortUrls Then
                            sCrumb &= "<a href=""" & NavigateUrl(TabId, "", ParamKeys.ForumId & "=" & ForumId) & """>" & ForumInfo.ForumName & "</a>"
                            sCrumb &= "|<a href=""" & NavigateUrl(TabId, "", ParamKeys.TopicId & "=" & TopicId) & """>" & TopicSubject & "</a>"
                        Else
                            sCrumb &= "<a href=""" & NavigateUrl(TabId, "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.ViewType & "=" & Views.Topics}) & """>" & ForumInfo.ForumName & "</a>"
                            sCrumb &= "|<a href=""" & NavigateUrl(TabId, "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.ViewType & "=" & Views.Topic, ParamKeys.TopicId & "=" & TopicId}) & """>" & TopicSubject & "</a>"
                        End If
                        If Environment.UpdateBreadCrumb(Page.Controls, sCrumb) Then
                            bcUpdated = True
                        End If

                    End If
                Else
                    Response.Redirect(NavigateUrl(TabId))
                End If
            End If
        End Sub
        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
            Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)
            MyBase.Render(htmlWriter)
            Dim html As String = stringWriter.ToString()
            If bcUpdated Then
                html = html.Replace("<div class=""afcrumb"">[AF:LINK:FORUMMAIN] > [AF:LINK:FORUMGROUP] > [AF:LINK:FORUMNAME]</div>", String.Empty)
            Else
                html = html.Replace("[AF:LINK:FORUMMAIN]", "<a href=""" & NavigateUrl(TabId) & """>[RESX:FORUMS]</a>")
                html = html.Replace("[AF:LINK:FORUMGROUP]", "<a href=""" & NavigateUrl(TabId, "", ParamKeys.GroupId & "=" & ForumInfo.ForumGroupId) & """>" & ForumInfo.GroupName & "</a>")
                html = html.Replace("[AF:LINK:FORUMNAME]", "<a href=""" & NavigateUrl(TabId, "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.ViewType & "=" & Views.Topics}) & """>" & ForumInfo.ForumName & "</a>")
            End If


            html = Utilities.LocalizeControl(html)

            writer.Write(html)
        End Sub

        Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Response.Redirect(NavigateUrl(CInt(Request.QueryString("TabId")), "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.ViewType & "=" & Views.Topic, ParamKeys.TopicId & "=" & TopicId}))
        End Sub

        Private Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
            Page.Validate()
            If Page.IsValid Then
                Dim sSubject As String = txtRecipSubject.Text
                Dim sEmail As String = txtRecipEmail.Text
                Dim sEmailName As String = txtRecipName.Text
                Dim sMessage As String = txtMessage.Text
                sSubject = Utilities.CleanString(PortalId, sSubject.Trim, False, EditorTypes.TEXTBOX, False, False, ModuleId, String.Empty, False)
                sMessage = Utilities.CleanString(PortalId, sMessage.Trim, False, EditorTypes.TEXTBOX, False, False, ModuleId, String.Empty, False)
                Dim sUrl As String = NavigateUrl(CInt(Request.QueryString("TabId")), "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId, ParamKeys.ViewType & "=confirmaction", ParamKeys.ConfirmActionId & "=" & ConfirmActions.SendToComplete})
                Try
                    Dim oEmail As New Email
                    If Not sMessage = String.Empty And Not sSubject = String.Empty Then
                        oEmail.SendNotification(UserInfo.Email, sEmail, sSubject, sMessage, sMessage.Replace(vbCrLf, "<br />"))
                    End If


                Catch ex As Exception
                    'Response.Redirect(NavigateUrl(CInt(Request.QueryString("TabId")), "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId, ParamKeys.ViewType & "=confirmaction", ParamKeys.ConfirmActionId & "=" & ConfirmActions.SendToFailed}))
                End Try
                Response.Redirect(sUrl)


            End If
        End Sub
    End Class
End Namespace
